package com.hwy.catering.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwy.catering.annotation.AccessLimit;
import com.hwy.catering.dto.DishDto;
import com.hwy.catering.entity.Dish;
import com.hwy.catering.entity.User;
import com.hwy.catering.service.DishService;
import com.hwy.catering.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "菜品管理接口")
@RestController
@RequestMapping("/cater/dish")
public class DishController {

    @Resource
    private DishService dishService;

    @ApiOperation("xlsx菜品导出")
    @GetMapping("/exportDish")
    public void exportDish(HttpServletResponse response) {
        dishService.exportDish(response);
    }

    @ApiOperation("批量删除菜品")
    @PostMapping("/deleteSelect")
    public R deleteSelect(@RequestBody List<Dish> dishList){
        R r = dishService.deleteSelect(dishList);
        return r;
    }

    @ApiOperation("删除菜品")
    @PostMapping("/deletedDish/{id}")
    public R deletedDish(@PathVariable(value = "id") Long id){
        R r = dishService.deletedDish(id);
        return r;
    }

    @ApiOperation("更改菜品信息")
    @PostMapping("/editDish/{id}")
    public R editDish(@PathVariable Long id,@RequestBody Dish dish){
        R r = dishService.editDish(id, dish);
        return r;
    }

    @ApiOperation("添加菜品")
    @PostMapping("/addDish")
    public R addDish(@RequestBody Dish dish){
        R r = dishService.addDish(dish);
        return r;
    }

    @ApiOperation("分页查找所有菜品")
    //接口限流10秒之内只能接收4次请求
    @AccessLimit(limit = 20,sec = 5)
    @PostMapping("/findDish/{page}/{limit}")
    public R findDish(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Integer page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Integer limit,
            @RequestBody Dish dish
    ){
        Page<Dish> pageParam = new Page<>(page, limit);
        R r = dishService.findDish(pageParam,dish);
        return r;
    }

    @ApiOperation("查找具体菜品")
    @GetMapping("/getByIdDish/{id}")
    public R getByIdDish(@PathVariable Long id){
        R r = dishService.getByIdDish(id);
        return r;
    }

    @ApiOperation("查找菜品选择器的数据")
    @GetMapping("/findDishOptions")
    public R findDishOptions(){
        R r = dishService.findDishOptions();
        return r;
    }
}
